package com.itheima.service.db.impl.testSoul;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.testSoul.Report;

import com.itheima.mapper.testSoul.ReportMapper;
import com.itheima.service.db.testSoul.ReportService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    //根据问卷id和用户id查找report
    @Override
    public Report findById(Long userId, Long questionnaireId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("questionnaire_id",questionnaireId);
        Report report = reportMapper.selectOne(wrapper);
        //Report reportMapperByUserId = reportMapper.findByUserId(userId,questionnaireId);
        return report;
    }

    //保存报告
    @Override
    public Report save(Report report) {
        reportMapper.insert(report);
        return report;
    }

    //更新报告
    @Override
    public void update(Report report) {
        reportMapper.updateById(report);
    }

    //根据id查找报告
    @Override
    public Report findOne(Long id) {
        Report report = reportMapper.selectById(id);
        return report;
    }


    //根据userId查找报告
    @Override
    public Map<String, Report> findByUserId(Long userId) {
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Report> reports = reportMapper.selectList(wrapper);
        Map<String , Report> map = new HashMap<>();
        for (Report report : reports) {
            map.put(report.getQuestionnaireId().toString(),report);
        }
        return map;
    }

    //根据报告id查找character_result相同的
    @Override
    public List<Report> findSimilar(Long id) {
        Report report = reportMapper.selectById(id);
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        wrapper.eq("character_result",report.getCharacterResult());
        List<Report> reports = reportMapper.selectList(wrapper);
        return reports;
    }
}
